<template>
  <el-select v-model="selectId" style="width: 260px;" filterable :clearable="false">
    <el-option v-for="item in arr" :key="item.id" :label="$formatNameCode(item.name, item.code)" :value="item.id">
    </el-option>
  </el-select>
</template>

<script>



export default {
  data() {
    return {
      selectId: '',
      dataMap: {},
      arr: [],

      userInfo: __LONG_USER__
    }
  },

  watch: {

    'selectId'(val, old) {


      let row = this.dataMap[val]
      if (!row) {
        return
      }

      if (this.userInfo.currentWarehouseId != val) {
        console.log(this.userInfo.currentWarehouseId,val)
        this.$message.success(this.$tt("tip_success_switched_to_refresh_page"))

        this.$ajax({
          url: '/ums-api/user/CookieRpc/changeCurrentWarehouse.json',
          data: { warehouseId: val },
          success: function (res) {

            // if( currentWH.domain && currentWH.domain.indexOf(window.location.host) == -1){
            //   window.location = currentWH.domain
            // }

            // if(warehouseDomain && warehouseDomain!=''){
            //   window.location = warehouseDomain
            // }

            window.setTimeout(()=>{
              window.location.reload()
            },100)

          }

        })

      }
    }
  },

  created() {

    let _this = this

    let currentWarehouseId = _this.userInfo.currentWarehouseId


    if (_this.userInfo && _this.userInfo.warehouses) {

      _this.userInfo.warehouses.forEach(e => {
        _this.dataMap[e.id] = e
      })

      _this.arr = _this.userInfo.warehouses

      if (currentWarehouseId && _this.dataMap[currentWarehouseId]) {
        _this.selectId = currentWarehouseId
      }

      if (_this.selectId == '' && _this.arr.length > 0) {
        _this.selectId = _this.arr[0].id
      }

    }

  },

  methods: {}
}
</script>
